/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import model.LevelManager;
import model.LevelSpecification;

/**
 *
 * @author meije_000
 */
public class LevelSelectionPaneel extends javax.swing.JPanel {

    /**
     * Creates new form LevelSelectionPaneel
     */
    LevelManager levelManager = new LevelManager();
    HoofdFrame hoofdframe;

    public LevelSelectionPaneel(HoofdFrame frame) {
        initComponents();
        levelManager = new LevelManager();
        hoofdframe = frame;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                                          
    private void messageDialog(ArrayList<String> messages) {
        String message = "";
        Iterator<String> it = messages.iterator();
        while (it.hasNext()) {
            message = message + it.next() + "\t";
        }
        JOptionPane.showMessageDialog(this, message);
    }

    private void messageDialog(String message) {
        JOptionPane.showMessageDialog(this, message);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jspLevels = new javax.swing.JScrollPane();
        lstLevels = new javax.swing.JList();
        btnPlay = new javax.swing.JButton();
        Back = new javax.swing.JButton();

        lstLevels.setModel(new javax.swing.AbstractListModel() {
            String[] strings = levelManager.getLevelStorageFileNames();
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jspLevels.setViewportView(lstLevels);

        btnPlay.setText("Play");
        btnPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlayActionPerformed(evt);
            }
        });

        Back.setText("Back");
        Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspLevels, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(Back)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnPlay)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPlay)
                    .addComponent(Back))
                .addGap(18, 18, 18)
                .addComponent(jspLevels, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayActionPerformed

        LevelSpecification tempLevelSpecification = levelManager.getLevelSpecificationByName((String) lstLevels.getModel().getElementAt(lstLevels.getSelectedIndex()));
        if (tempLevelSpecification != null) {
            GamePaneel gamepaneel = new GamePaneel(tempLevelSpecification,hoofdframe);
            hoofdframe.setContentPanel(gamepaneel);
        } else {
            this.messageDialog(this.levelManager.getMsg());
            this.levelManager.clearMsg();
        }
    }//GEN-LAST:event_btnPlayActionPerformed

    private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
            /*
             * maak een nieuwe HomePaneel aan, Geef de instance van het hoofdframe mee.
             * Run uit instance hoofdframe setContentPanel om het nieuwe homepaneel in het frame te zetten
             */
            HomePaneel homepaneel = new HomePaneel(hoofdframe);
            hoofdframe.setContentPanel(homepaneel);
    }//GEN-LAST:event_BackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Back;
    private javax.swing.JButton btnPlay;
    private javax.swing.JScrollPane jspLevels;
    private javax.swing.JList lstLevels;
    // End of variables declaration//GEN-END:variables
   
}

